﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using NicoView.Model.Service;

using NicoView.Model.Service.Results;
using GalaSoft.MvvmLight.Command;
using NicoView.ViewModel.Message;
using NicoView.Model;
using NicoView.ViewModel.Localize;


namespace NicoView.ViewModel
{
    public class NicoViewIdInputViewModel : NicoViewViewModelBase
    {

        public NicoViewIdInputViewModel()
        {
            connectCommand = null;
            inputId = "";
            ConnectSelections = new ObservableCollection<NicoViewConnectionSelectionViewModel>();

        }

        public ObservableCollection<NicoViewConnectionSelectionViewModel> ConnectSelections { get; private set; }

        private ICommand connectCommand;

        private string inputId;

        public string InputId
        {
            get
            {
                return this.inputId;
            }

            set
            {
                this.Set(() => this.InputId, ref this.inputId, value);
            }
        }

        public void Loded()
        {
            if(!IsInDesignMode)this.InternalApplication.Instance.LiveConnected += args =>
            {

                Add(args.LiveService.LiveServiceInfo);

   
            };
        }

        public ICommand ConnectCommand
        {

            get
            {
                if (connectCommand == null)
                {
                    connectCommand = new RelayCommand(async () =>
                    {
                        try
                        {
                            this.MessengerInstance.Send(new NicoViewIdInputConnectionExecuteMessage());
                            var rslt = await NicoViewApplication.Current.Instance.ConnectServiceAsync(this.InputId);
                            if (!rslt.Succeed && rslt.Code != NiconicoConnectionResultCode.Connecting)
                            {
                                this.MessengerInstance.Send(new NicoViewConnectionFailedMessage(rslt.Message));
                            }
                        }
                        catch
                        {
                            this.MessengerInstance.Send(new NicoViewConnectionFailedMessage( Langage.Current.InvalidIdMessage));
                        }
                        finally
                        {
                            this.MessengerInstance.Send(new NicoViewIdInputConnectionExecutedMessage());
                        }

                        

                        
                    });
                }

                return connectCommand;
            }
        }

        public void Add(INicoConnectSelection selection)
        {
            var enumrslt = from connectionSelect in this.ConnectSelections
                           where connectionSelect.Id == selection.Id
                           select connectionSelect;

            if (enumrslt.Count() <= 0)
            {
                var selectionItem = new NicoViewConnectionSelectionViewModel(selection);
                this.ConnectSelections.Add(selectionItem);
                this.MessengerInstance.Send(new NicoViewAddSelectionMessage(selectionItem, this.ConnectSelections.IndexOf(selectionItem)));
            }
            else
            {
                this.MessengerInstance.Send(new NicoViewAddSelectionMessage(enumrslt.First(), this.ConnectSelections.IndexOf(enumrslt.First())));
            }

            
        }

    }
}
